<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:student:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleClass"
          v-hasPermi="['student:student:export']"
        >转班</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleState1"
          v-hasPermi="['student:student:edit']"
        >状态修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex==1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="学历" align="center" prop="edication" />
      <el-table-column label="专业" align="center" prop="speciality" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学校" align="center" prop="school" />
      <el-table-column label="电话" align="center" prop="tel" />
      <el-table-column label="父母电话" align="center" prop="parentTel" />
      <el-table-column label="学生状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state==0">毕业未就业</span>
          <span v-if="scope.row.state==1">在校学习</span>
          <span v-if="scope.row.state==2">留级在读</span>
        </template>
      </el-table-column>
      <el-table-column label="模拟面试" align="center" prop="mockInterview">
        <template slot-scope="scope">
          <span v-if="scope.row.mockInterview==0">未测试</span>
          <span v-if="scope.row.mockInterview==1">测试通过</span>
          <span v-if="scope.row.mockInterview==2">测试未通过</span>
        </template>
      </el-table-column>
      <el-table-column label="结业考试" align="center" prop="finalTest">
        <template slot-scope="scope">
          <span v-if="scope.row.finalTest==0">未测试</span>
          <span v-if="scope.row.finalTest==1">测试通过</span>
          <span v-if="scope.row.finalTest==2">测试未通过</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['student:student:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="handleFollow(scope.row)"
            v-hasPermi="['student:student:follow']"
          >关注</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  留级转班  -->
    <el-dialog :title="title" :visible.sync="opens" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form1.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form1.classId" placeholder="请选择">
            <el-option
              v-for="item in classes"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生状态" prop="state">
          <el-select v-model="form1.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="name">
          <el-input v-model="form1.notes" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <!--  关注按钮  -->
    <el-dialog :title="title" :visible.sync="opense" width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form2.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="负责老师" prop="classId">
          <el-select v-model="form2.teacherChargeId" placeholder="请选择">
            <el-option
              v-for="item in classes"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="困难系数" prop="difficultyCoefficient">
          <el-select v-model="form2.difficultyCoefficient" placeholder="请选择">
            <el-option
              v-for="item in optionses"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="name">
          <el-input v-model="form2.notes" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
    //状态修改
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form3" :model="form3" :rules="rules" label-width="80px">
        <el-form-item label="学生状态" prop="state">
          <el-select v-model="form3.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="mockInterview">
          <el-select v-model="form3.mockInterview" placeholder="请选择">
            <el-option
              v-for="item in optionss"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业考试" prop="finalTest">
          <el-select v-model="form3.finalTest" placeholder="请选择">
            <el-option
              v-for="item in optionsss"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm3">确 定</el-button>
        <el-button @click="cancel3">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio class="radio" :label="1">男</el-radio>
            <el-radio class="radio" :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学历" prop="edication">
          <el-input v-model="form.edication" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="专业" prop="speciality">
          <el-input v-model="form.speciality" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择">
            <el-option
              v-for="item in classes"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="父母电话" prop="parentTel">
          <el-input v-model="form.parentTel" placeholder="请输入父母电话" />
        </el-form-item>
        <el-form-item label="学生状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="mockInterview">
          <el-select v-model="form.mockInterview" placeholder="请选择">
            <el-option
              v-for="item in optionss"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业考试" prop="finalTest">
          <el-select v-model="form.finalTest" placeholder="请选择">
            <el-option
              v-for="item in optionsss"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudent,
  getStudent,
  delStudent,
  addStudent,
  updateStudent,
  getClass,
  changeClass, followStudent
} from "@/api/student/student";

export default {
  name: "student",
  data() {
    return {
      optionses: [{
        value: 0,
        label: '一般困难'
      }, {
        value: 1,
        label: '非常困难'
      }],
      classes:[],
      options: [{
        value: 0,
        label: '毕业未就业'
      }, {
        value: 1,
        label: '在校学习'
      }, {
        value: 2,
        label: '留级在读'
      }],
      state: '',
      optionss: [{
        value: 0,
        label: '未测试'
      }, {
        value: 1,
        label: '测试通过'
      }, {
        value: 2,
        label: '测试未通过'
      }],
      mockInterview: '',
      optionsss: [{
        value: 0,
        label: '未测试'
      }, {
        value: 1,
        label: '测试通过'
      }, {
        value: 2,
        label: '测试未通过'
      }],
      finalTest: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      opens: false,
      opense: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        age: null,
        edication: null,
        speciality: null,
        classId: null,
        school: null,
        tel: null,
        parentTel: null,
        state: null,
        mockInterview: null,
        finalTest: null
      },
      // 表单参数
      form: {},
      form1: {},
      form2: {},
      form3: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getClasses() {
      this.loading = true;
      getClass(this.queryParams).then(res=>{
        this.classes = res.data
        this.loading = false;
      }).catch(res=>{
        this.$message.error("网络繁忙");
      })
    },
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.opens = false;
      this.reset();
    },
    cancel2() {
      this.opense = false;
      this.reset();
    },
    cancel3() {
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        age: null,
        edication: null,
        speciality: null,
        classId: null,
        school: null,
        tel: null,
        parentTel: null,
        state: null,
        mockInterview: null,
        finalTest: null
      };
      this.form1 = {
        name: null,
        classId: null,
        state: null,
        notes: null,
      };
      this.form2 = {
        name: null,
        classId: null,
        state: null,
        notes: null,
      };
      this.form3 = {
        state: null,
        mockInterview: null,
        finalTest: null,
      };
      this.resetForm("form");
      this.resetForm("form1");
      this.resetForm("form2");
      this.resetForm("form3");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getClasses();
      this.form.sex = 1;
      this.open = true;
      this.title = "添加学生";
    },
    handleClass() {
      this.reset();
      this.getClasses();
      this.opens = true;
      this.title = "留级转班";
    },
    handleFollow(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form2 = response.data;
        this.opense = true;
        this.title = "重点关注";
      });
    },
    handleState1(row) {
      const id = row.id || this.ids
      //获取选中的行
      if (!id || id.length < 1) {
        this.$message({message: '老铁，你不选中数据，臣妾上不了啊....', type: 'error'});
        return;
      }
      if (id.length !== 1) {
        this.$message({message: '只能选择一条数据', type: 'error'});
        return;
      }
      this.reset();
      getStudent(id).then(response => {
        this.form3 = response.data;
        this.open1 = true;
        this.title = "状态修改";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getClasses();
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
    handleState(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //留级转班
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          changeClass(this.form1).then(response => {
              this.$modal.msgSuccess("学生转班成功");
              this.opens = false;
              this.getList();
            });
          }
      });
    },
    //重点关注
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          followStudent(this.form2).then(response => {
            this.$modal.msgSuccess("重点关注成功");
            this.opense = false;
            this.getList();
          });
        }
      });
    },
    submitForm3() {
      this.$refs["form3"].validate(valid => {
        if (valid) {
          updateStudent(this.form3).then(response => {
            this.$modal.msgSuccess("状态修改成功");
            this.open1 = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
